package com.ruoyix.alipay.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.easysdk.factory.Factory;

import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.common.Client;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.ruoyix.alipay.config.AliPayConfig;
import com.ruoyix.alipay.domain.AliPay;
import com.ruoyix.alipay.service.AliPayService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * @Author
 * @Date Created in  2023/5/5 15:23
 * @DESCRIPTION:
 * @Version V1.0
 */
@RestController
@RequestMapping("/alipay")
@Transactional(rollbackFor = Exception.class)
public class AliPayController {

    @Resource
    private AliPayService aliPayService;

    private static final String GATEWAY_URL ="https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private static final String FORMAT ="JSON";
    private static final String CHARSET ="utf-8";
    private static final String SIGN_TYPE ="RSA2";


    //PC支付
    @SaIgnore
    @GetMapping("/pcPay") // 前端路径参数格式?subject=xxx&traceNo=xxx&totalAmount=xxx
    public void pcPay(AliPay aliPay, HttpServletResponse httpResponse) throws Exception {
        aliPayService.pcPay(aliPay, httpResponse);
    }


    //扫码支付
    @GetMapping("/payQrCode")
    public String payQrCode(AliPay aliPay,HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        return aliPayService.payQrCode(aliPay, httpServletRequest, httpServletResponse);
    }




    @SaIgnore
    @PostMapping("/notify")  // 注意这里必须是POST接口
    public String payNotify(HttpServletRequest request) throws Exception {
        return aliPayService.payNotify(request);
    }



    @PostMapping("/queryTrade")
    public JSONObject queryTrade(AliPay aliPay) throws AlipayApiException {
        return aliPayService.queryTrade(aliPay);
    }

    @PostMapping("/TradeRefund")
    public JSONObject TradeRefund(AliPay aliPay) throws AlipayApiException {
        return aliPayService.TradeRefund(aliPay);
    }


}
